/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.view;

import Utils.MyOwnFocusTraversalPolicy;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.Box;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import samoloty.controler.Controler;

/**
 *
 * @author frankowp
 */
public class ConfirmPanel extends ContentPanel {

    private MyButton yesBtn;
    private MyButton noBtn;
    private MyLabel titleLab;
    private Controler controler;
    private boolean mode;
    private static int EXIT_MODE = 0;
    private static int RESET_MODE = 1;
    private MyOwnFocusTraversalPolicy travelsalPolicy;

    public ConfirmPanel() {
        super();
        initPanel();
    }

    private void initPanel() {


        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int width = 250;
        int height = 120;
        this.setBounds((screen.width - width) / 2, (screen.height - height) / 2, width, height);
        ActionMap actionMap = getActionMap();
        InputMap inputMap = getInputMap();

        KeyStroke noKey = KeyStroke.getKeyStroke("ESCAPE");
        KeyStroke yesKey = KeyStroke.getKeyStroke("ENTER");
        inputMap.put(noKey, "noK");
        inputMap.put(yesKey, "yesK");
        Action noAA = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                controler.getView().setRightPanelVisible(true);
                controler.setDefaultPanel();
            }
        };
        Action yesAA = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                if (mode) {
                    controler.resetProgram();
                } else {
                    controler.exitProgram();
                }
            }
        };
        actionMap.put("noK", noAA);
        actionMap.put("yesK", yesAA);
        setBounds((768 / 2) - (250 / 2), (576 / 2) - (120 / 2), 250, 120);
        setPreferredSize(new Dimension(250, 120));
        setMinimumSize(new Dimension(250, 120));
        titleLab = new MyLabel("");
        titleLab.setBold();
        add(titleLab);
        add(Box.createRigidArea(new Dimension(250, 20)));
        yesBtn = new MyButton("YES");
        yesBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (mode) {
                    controler.resetProgram();
                } else {
                    controler.exitProgram();
                }
            }
        });
        add(yesBtn);
        noBtn = new MyButton("NO");
        noBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                controler.getView().setRightPanelVisible(true);
                controler.setDefaultPanel();
            }
        });
        add(noBtn);
        yesBtn.requestFocus();

        //focus order
        Vector<Component> focusOrder = new Vector<Component>();
        focusOrder.add(yesBtn);
        focusOrder.add(noBtn);

        travelsalPolicy = new MyOwnFocusTraversalPolicy(focusOrder);
        this.setFocusTraversalPolicy(travelsalPolicy);
        this.setFocusTraversalPolicyProvider(true);
        this.setFocusCycleRoot(true);
        try {
            controler.getView().setRightPanelVisible(false);
        } catch (NullPointerException e) {
        }
    }

    public void reset() {
    }

    public void setControler(Controler aThis) {
        this.controler = aThis;
    }

    public void setResetAction() {
        controler.getView().setRightPanelVisible(false);
        titleLab.setText("RESET THE PROGRAM?");
        mode = true;
        yesBtn.requestFocus();
    }

    public void setExitAction() {
        controler.getView().setRightPanelVisible(false);
        titleLab.setText("EXIT THE PROGRAM?");
        mode = false;
        yesBtn.requestFocus();
    }
}
